﻿using QuanLyThuVien.DTO;
using QuanLyThuVien.Supporter;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QuanLyThuVien.DAL
{
    //----------------------------------------------------------------------
    //Desc: class kết nối với bảng PHIEUNHAP
    //----------------------------------------------------------------------
    public class DALPhieuNhap
    {
        //----------------------------------------------------------------------
        //Desc: constructor mặc định
        //----------------------------------------------------------------------
        public DALPhieuNhap()
        { }

        //----------------------------------------------------------------------
        //Desc: chuyển dòng dữ liệu thành Phiếu Nhập
        //----------------------------------------------------------------------
        public DTOPhieuNhap ConvertToPhieuNhap(DataRow row)
        {
            DTOPhieuNhap phieunhap = new DTOPhieuNhap();
            phieunhap.MaPhieuNhap = row["MaPhieuNhap"].ToString();
            try
            {
                phieunhap.NgayNhap = DateTime.Parse(row["NgayNhap"].ToString());
            }
            catch { }
            try
            {
                phieunhap.TongTriGia = Double.Parse(row["TongTriGia"].ToString());
            }
            catch { }
            return phieunhap;
        }

        //----------------------------------------------------------------------
        //Desc: lấy danh sách phiếu nhập
        //----------------------------------------------------------------------
        public DataTable LayDanhSachPhieuNhap()
        {
           if (DatabaseManager.DbConnection == null)
                return null;
            string spName = "spPhieuNhap_LayDanhSachPhieuNhap";
            return DatabaseManager.DbConnection.ExecuteStoredProcedure(spName);
        }

        //----------------------------------------------------------------------
        //Desc: lấy phiếu nhập
        //----------------------------------------------------------------------
        public DTOPhieuNhap LayPhieuNhap(string maphieunhap)
        {
           if (DatabaseManager.DbConnection == null)
                return null;
            string spName = "spPhieuNhap_LayPhieuNhap";
            SqlParameter sqlprMaPhieuNhap = new SqlParameter("@MaPhieuNhap", SqlDbType.VarChar, 15)
            { Value = maphieunhap };
            DataTable data = DatabaseManager.DbConnection.ExecuteStoredProcedure(spName, sqlprMaPhieuNhap);
            if (data == null || data.Rows.Count == 0)
                return null;
            return ConvertToPhieuNhap(data.Rows[0]);
        }

        //----------------------------------------------------------------------
        //Desc: đếm phiếu nhập
        //----------------------------------------------------------------------
        public int DemPhieuNhap()
        {
           if (DatabaseManager.DbConnection == null)
                return -1;
            string spName = "spPhieuNhap_DemPhieuNhap";
            object obj = DatabaseManager.DbConnection.ExecuteStoredProcedureScalar(spName);
            if (obj == null)
                return -1;
            int dem;
            try
            {
                dem = (int)obj;
            }
            catch { return -1; }
            return dem;
        }

        //----------------------------------------------------------------------
        //Desc: đếm mã phiếu nhập
        //----------------------------------------------------------------------
        public int DemMaPhieuNhap(string maphieunhap)
        {
           if (DatabaseManager.DbConnection == null)
                return -1;
            string spName = "spPhieuNhap_DemMaPhieuNhap";
            SqlParameter sqlprMaPhieuNhap = new SqlParameter("@MaPhieuNhap", SqlDbType.VarChar, 15) { Value = maphieunhap };
            object obj = DatabaseManager.DbConnection.ExecuteStoredProcedureScalar(spName, sqlprMaPhieuNhap);
            if (obj == null)
                return -1;
            int dem;
            try
            {
                dem = (int)obj;
            }
            catch { return -1; }
            return dem;
        }

        //----------------------------------------------------------------------
        //Desc: cập nhật phiếu nhập
        //----------------------------------------------------------------------
        public bool CapNhat(DTOPhieuNhap phieunhap)
        {
           if (DatabaseManager.DbConnection == null)
                return false;
            string spName = "spPhieuNhap_CapNhatPhieuNhap";
            SqlParameter sqlprThaoTac = new SqlParameter("@ThaoTac", SqlDbType.Int) { Value = 1 };
            SqlParameter sqlprMaPhieuNhap = new SqlParameter("@MaPhieuNhap", SqlDbType.VarChar, 15)
            { Value = phieunhap.MaPhieuNhap };
            SqlParameter sqlprNgayNhap = new SqlParameter("@NgayNhap", SqlDbType.Date) { Value = phieunhap.NgayNhap };
            SqlParameter sqlprTongTriGia = new SqlParameter("@TongTriGia", SqlDbType.Money) { Value = phieunhap.TongTriGia};
            return DatabaseManager.DbConnection.ExecuteStoredProcedureNonQuery(spName, sqlprThaoTac, sqlprMaPhieuNhap,
                sqlprNgayNhap, sqlprTongTriGia);
        }
    }
}
